<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>加入表单元素 - 数据表格</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>加入表单元素</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">系统配置参数列表</div>
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-role" data-type="batchdel">删除</button>
                        <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                    </div>

                    <table id="LAY-system-back" lay-filter="LAY-system-back"></table>

                    <script type="text/html" id="userTpl">
                        <a lay-href="/admin/user/{{ d.userId }}" lay-tips="{{ d.nickname }}的详细信息" lay-text="用户详情" class="fly-link">{{ d.nickname }}</a>
                    </script>

                    <script type="text/html" id="test-table-switchTpl">
                        <!-- 这里的 checked 的状态只是演示 -->
                        <input type="checkbox" name="sex" lay-skin="switch" lay-text="女|男"
                               lay-filter="test-table-sexDemo"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.id==
                               10003 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="test-table-checkboxTpl">
                        <!-- 这里的 checked 的状态只是演示 -->
                        <input type="checkbox" name="lock" title="锁定" lay-filter="test-table-lockDemo"
                               value="{{d.id}}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.id== 10006
                               ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="system-table-setTpl">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
<script>
    layui.config({
        base: '${global_templets_skin}/systemstyle/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form'], function () {
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        table.render({
            elem: '#LAY-system-back'
            , url: '/admin/system/system_listData.do'
            , cellMinWidth: 80
            , cols: [[
                {type: 'numbers'}
                , {type: 'checkbox'}
                , {field: 'id', title: 'ID', width: 180, unresize: true, sort: true}
                , {field: 'keyCode', title: '系统参数KEY'}
                , {field: 'keyValue', title: '参数Value', minWidth: 120, sort: true}
                , {field: 'description', title: '配置说明', minWidth: 120, sort: true}
                ,{title:'操作', align:'center', templet: '#system-table-setTpl', unresize: true}
            ]]
            ,id: 'roleReload'
            , page: true
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('roleReload')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                deList=[];
                checkData.forEach(function(n,i){
                    deList.push(n.id);
                })
                layer.confirm('确定删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function() {
                    $.ajax({
                        type: "POST"
                        ,dataType: 'json'
                        , url: '/admin/system/del_system.do' //实际使用请改成服务端真实接口
                        , data: "ids=" + deList
                        , done: function (e) {
                            if (e.code == "0") {
                                layer.msg('成功');
                            }else {
                                layer.msg(e.message);
                                return;
                            }
                        }
                    });
                    table.reload('roleReload');
                    layer.msg('已删除', {icon: 1});
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加系统配置'
                    ,content: '/admin/system/add_system.do'
                    ,maxmin: true
                    ,area: ['500px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:'/admin/system/add_system.do',
                                type:'post',
                                dataType: 'json',
                                data: data.field,
                                success:function(e){
                                    if (e.code == "0") {
                                        layer.msg('成功');
                                    }else {
                                        layer.msg(e.message);
                                        return;
                                    }
                                }
                            })
                            table.reload('roleReload'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        };

        //监听行工具事件
        table.on('tool(LAY-system-back)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除当前配置吗？', function(index){
                    $.ajax({
                        type: "POST"
                        ,dataType: 'json'
                        , url: '/admin/system/del_system.do' //实际使用请改成服务端真实接口
                        , data: "ids=" + data.id
                        , done: function (e) {
                            if (e.code == "0") {
                                layer.msg('成功');
                            }else {
                                layer.msg(e.message);
                                return;
                            }
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改配置'
                    ,content: '/admin/system/update_system.do?id='+data.id
                    ,maxmin: true
                    ,area: ['500px', '450px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:'/admin/system/update_system.do',
                                type:'post',
                                dataType: 'json',
                                data: data.field,
                                success:function(e){
                                    if (e.code == "0") {
                                        layer.msg('成功');
                                    }else {
                                        layer.msg(e.message);
                                        return;
                                    }
                                }
                            })
                            table.reload('roleReload'); //数据刷新
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        });

        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>